let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchants: {},
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.shijian();
    let agent = wx.getStorageSync('agent');
    let position = wx.getStorageSync('position');
    let userId = wx.getStorageSync("userId");
    this.setData({
      agent,
      position,
      userId
    });
    this.onPullDownRefresh();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  shijian:function(){

    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;

  this.setData({
    currentdate

  })
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onPullDownRefresh();
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  // 跳转到商家详情
  toDetail (e) {
    let shopId = e.currentTarget.dataset.id;
    let distance = e.currentTarget.dataset.distance;
    wx.navigateTo({
      url: `/pages/merchantsList/detail/detail?shopId=${shopId}&distance=${distance}`,
    })
  },
  // 打电话
  phoneCall (param) {
    wx.makePhoneCall({
      phoneNumber: param.target.dataset.phone,
    })
  },
  // 跳转到商家加盟
  toJoin (e) {
    wx.navigateTo({
      url: '/pages/merchantsList/join/join',
    })
  },
  initData() {
    const merchants = {
      items: [],
      total: '',
      params: {
        page: 1,
        pageCount: 5
      }
    }
    this.setData({
      merchants: merchants
    })
  },
  getList(cb) {
    let { merchants, agent, position } = this.data;
    let params = merchants.params;
    let location = position.location;
    params.sellerId = agent.id;
    params.userId = this.data.userId;
    params.lng = location.split(',')[0];
    params.lat = location.split(',')[1];
    params.dataTime = this.data.currentdate
    App.HttpService.getMerchants(params).then(res => {
      cb && cb()
      let data = res.data.data
      merchants.items = [...merchants.items, ...data.shopList]
      if (data.shopList.length > 0) {
        params.page = params.page + 1
        merchants.total = data.counts
      }
      console.log(merchants);
      this.setData({ merchants })
    }, e => {
      cb && cb()
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    this.initData()

    this.getList(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let merchants = this.data.merchants
    if (merchants.items.length === merchants.total) {
      return
    }
    this.getList()
  },
})